package com.authine.cloudpivot.ext.vo.i;

import com.authine.cloudpivot.ext.constants.TaxRate;
import com.authine.cloudpivot.ext.enums.finance.InvoiceStatusEnum;
import com.authine.cloudpivot.ext.enums.finance.PaymentStatusEnum;
import com.authine.cloudpivot.ext.enums.finance.ReconciliationStatusEnum;
import com.authine.cloudpivot.ext.utils.BigDecimalUtils;
import com.authine.mvp.app.launcher.domain.SelectorFormat;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import org.apache.commons.lang.StringUtils;
import org.springframework.util.ObjectUtils;

import java.math.BigDecimal;
import java.util.List;


@Data
@AllArgsConstructor
@NoArgsConstructor
public class IOrderCustomerVO {

    /**
     * ID
     */
    private String id;
    /**
     * 创建人
     */
    private String createdBy;
    /**
     * 创建人部门
     */
    private String createdDept;
    /**
     * 创建时间
     */
    private String createdTime;
    /**
     * 拥有者
     */
    private String owner;
    /**
     * 拥有者部门
     */
    private String ownerDept;
    /**
     * 修改人
     */
    private String modifiedBy;
    /**
     * 修改时间
     */
    private String modifiedTime;
    /**
     * 单据号
     */
    private String document;
    /**
     * 单据状态
     */
    private String status;
    /**
     * 付款方式
     */
    private String payment_method;
    /**
     * 下单时间（下单付款的时间）
     */
    private String place_order_time;
    /**
     * ERP PO
     */
    private String erp_po;
    /**
     * 收货人
     */
    private String receiver;
    /**
     * 电话
     */
    private String phone;
    /**
     * 邮编
     */
    private String postal;
    /**
     * 详细地址
     */
    private String address;
    /**
     * 运费
     */
    private BigDecimal freight;
    /**
     * 税前商品合计
     */
    private BigDecimal pretax_total;
    /**
     * 税后商品合计
     */
    private BigDecimal total;
    /**
     * 税后商品运费共计
     */
    private BigDecimal amount;
    /**
     * vmi消耗单ids
     */
    private String vmi_ids;
    /**
     * 下单人用户ID
     */
    private SelectorFormat place_order_user_id;
    /**
     * 下单人名称
     */
    private String place_order_fullname;
    /**
     * 客户组织id
     */
    private SelectorFormat organization_id;
    /**
     * 客户组织名称
     */
    private String organization_name;
    /**
     * 外部来源客户id
     */
    private String client_id;
    /**
     * 外部来源用户id
     */
    private String client_user_id;
    /**
     * 外部来源订单id
     */
    private String client_order_id;
    /**
     * 扩展属性（{\"OS订单号\":\"\"
     * \"实际下单人\":\"\"}）
     */
    private String extends_attr;
    /**
     * 是否已启用（true=启用、false=未启用）
     */
    private Boolean enabled;
    /**
     * 售后状态（未售后、售后中、已售后）
     */
    private String after_sale;
    /**
     * 订单类型（平台订单、外部订单、商城订单、代购订单、VMI订单）
     */
    private String type;
    /**
     * 未税运费
     */
    private BigDecimal pretax_freight;
    /**
     * 税前商品运费共计
     */
    private BigDecimal pretax_amount;
    /**
     * 税额
     */
    private BigDecimal tax_payable;
    /**
     * 审批任务状态
     */
    private String task_ver;
    /**
     * 可接受最迟交货日
     */
    private String accept_delivery_time;
    /**
     * 老状态
     */
    private BigDecimal old_status;
    /**
     * 订单类型
     */
    private String source;
    /**
     * 客户订单状态
     */
    private String order_customer_status;
    /**
     * 审批人
     */
    private String approval_by;
    /**
     * 审批意见
     */
    private String approval_opinion;
    /**
     * 外部来源客户(第三方下单人)
     */
    private String client_user;
    /**
     * 实际下单人
     */
    private String actual_order;
    /**
     * 运费税额
     */
    private BigDecimal tax_payable_freight;
    /**
     * 账期
     */
    private BigDecimal accountPeriod;


    /**
     * 购物车id
     */
    private List<String> cartIds;
    /**
     * 税率
     */
    private BigDecimal tax_rate;
    /**
     * 运费税率
     */
    private BigDecimal tax_rate_freight;

    /**
     * 供应商数量
     */
    private Integer vendorCount;
    /**
     * 商品数量
     */
    private Integer goodsCount;
    /**
     * os订单号
     */
    private String client_os;


    private String username; // 登录名
    private String platformId; //平台组织id
    private String platformName;//平台组织名称

    private ReconciliationStatusEnum reconciliationStatus; //对账状态
    private InvoiceStatusEnum invoiceStatus; //开票状态
    private PaymentStatusEnum paymentStatus; //结算状态


    public void setId(final String id) {

        if (StringUtils.isNotEmpty(id)) {
            if (!ObjectUtils.isEmpty(getOrderVendorVO())) {
                getOrderVendorVO().stream().forEach(t -> t.setOrder_customer_id(id));
            }
            if (!ObjectUtils.isEmpty(getOrderCustomerGoodsVO())) {
                getOrderCustomerGoodsVO().stream().forEach(t -> t.setOrder_customer_id(id));
            }
        }


        this.id = id;
    }

    /**
     * 供应商列表
     */
    private List<IOrderVendorVO> orderVendorVO;

    /**
     * 客户订单商品
     */
    private List<IOrderCustomerGoodsVO> orderCustomerGoodsVO;


    public BigDecimal getTaxRate() {
        if (null == tax_rate) {
            tax_rate = TaxRate.Tax_Rate_0_16;
        }

        return tax_rate;
    }

    public BigDecimal getTaxRateFreight() {
        if (null == tax_rate_freight) {
            tax_rate_freight = TaxRate.Tax_Rate_0_16;
        }
        return tax_rate_freight;
    }

    public BigDecimal getTaxPayable() {
        tax_payable = BigDecimal.ZERO;
        if (getVendorCount() > 0) {
            for (IOrderVendorVO item : getOrderVendorVO()) {
                if (null != item.getTax_payable())
                    tax_payable = BigDecimalUtils.add(tax_payable, item.getTax_payable());
            }
        }
        return tax_payable;
    }


    public BigDecimal getTaxPayableFreight() {
        tax_payable_freight = BigDecimal.ZERO;
        ;
        if (getVendorCount() > 0) {
            for (IOrderVendorVO item : getOrderVendorVO()) {
                if (null != item.getTax_payable_freight())
                    tax_payable_freight = BigDecimalUtils.add(tax_payable_freight, item.getTax_payable_freight());
            }
        }
        return tax_payable_freight;
    }


    public BigDecimal getPretaxFreight() {
        pretax_freight = BigDecimal.ZERO;
        if (getVendorCount() > 0) {
            for (IOrderVendorVO item : getOrderVendorVO()) {
                if (null != item.getPretax_freight())
                    pretax_freight = BigDecimalUtils.add(pretax_freight, item.getPretax_freight());
            }
        }
        return pretax_freight;
    }


    public BigDecimal getPretaxTotal() {
        pretax_total = BigDecimal.ZERO;
        if (getVendorCount() > 0) {
            for (IOrderVendorVO item : getOrderVendorVO()) {
                if (null != item.getPretax_total())
                    pretax_total = BigDecimalUtils.add(pretax_total, item.getPretax_total());
            }
        }
        return pretax_total;
    }


    public BigDecimal getPretaxAmount() {
        pretax_amount = BigDecimal.ZERO;
        if (getVendorCount() > 0) {
            for (IOrderVendorVO item : getOrderVendorVO()) {
                if (null != item.getPretax_amount())
                    pretax_amount = BigDecimalUtils.add(pretax_amount, item.getPretax_amount());
            }
        }
        return pretax_amount;
    }

    public BigDecimal getFreight() {
        freight = BigDecimal.ZERO;
        if (getVendorCount() > 0) {
            for (IOrderVendorVO item : getOrderVendorVO()) {
                if (null != item.getFreight())
                    freight = BigDecimalUtils.add(freight, item.getFreight());
            }
        }
        return freight;
    }

    public BigDecimal getTotal() {
        total = BigDecimal.ZERO;
        if (getVendorCount() > 0) {
            for (IOrderVendorVO item : getOrderVendorVO()) {
                if (null != item.getTotal())
                    total = BigDecimalUtils.add(total, item.getTotal());
            }
        }
        return total;
    }


    public BigDecimal getAmount() {
        amount = BigDecimal.ZERO;
        if (getVendorCount() > 0) {
            for (IOrderVendorVO item : getOrderVendorVO()) {
                if (null != item.getAmount())
                    amount = BigDecimalUtils.add(amount, item.getAmount());
            }
        }
        return amount;
    }

    public Integer getVendorCount() {
        return vendorCount = null != getOrderVendorVO() && !getOrderVendorVO().isEmpty() ? getOrderVendorVO().size() : 0;
    }

    public Integer getGoodsCount() {
        goodsCount = 0;
        if (getVendorCount() > 0) {
            for (IOrderVendorVO item : getOrderVendorVO()) {
                if (null != item.getGoodsCount()) goodsCount += item.getGoodsCount();
            }
        }
        return goodsCount;
    }


    public void setOrderVendorVO(List<IOrderVendorVO> orderVendorVO) {
        this.orderVendorVO = orderVendorVO;
        this.getTaxRate();
        this.getTaxRateFreight();
        this.getTaxPayable();
        this.getTaxPayableFreight();
        this.getPretaxFreight();
        this.getFreight();
        this.getPretaxTotal();
        this.getTotal();
        this.getPretaxAmount();
        this.getAmount();
    }


}
